package com.db.bankapp.network.server;

import com.db.bankapp.domain.Bank;
import com.db.bankapp.service.BankDataLoaderService;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created by Student on 22.08.2014.
 */


/*
    /**
     * Return and prints maximum amount to withdraw in bank
     * @param bank in which to search
     * @return maximum amount to withdraw in bank
     */
public class BankServer {
    public static void startServer(Bank bank) {
        ServerSocket listener = null;
        try {
            listener = new ServerSocket(13000);
            while(true) {
                Socket socket = listener.accept();
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                while (true) {
                    String message = (String) reader.readLine();
                    if (message.split(" ")[0].equals("add")) {
                        BankDataLoaderService.feedBank(bank, new Scanner(message.split(" ")[1]));
                        writer.write("Server has added the client\n");
                        writer.flush();
                    } else {
                        writer.write("Unknown command\n");
                        writer.flush();
                    }


                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
